package com.sxfq.loansupermarket.backsystem.modular.IntegralMall.service.Impl;

import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.entity.SendMessageInfo;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.entity.image;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.entity.text;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.WebChatConfig;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.WebChatUtil;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.WechatUserInfoEntity;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.WxTokenInfo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.service.ISendMessageService;

@Service
@Transactional
public class SendMessageServiceImpl implements ISendMessageService {
    @Autowired
    private SqlMapper sqlMapper;

   public void sendTextMessage(String content, String openid) throws Exception {
        String access_token = getAccessToken();
        SendMessageInfo sendMessageInfo = new SendMessageInfo();
        text text = new text();
        sendMessageInfo.setTouser(openid);
        sendMessageInfo.setMsgtype("text");
        text.setContent(content);
        sendMessageInfo.setText(text);
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;
        String jsonString = JSONObject.fromObject(sendMessageInfo).toString().replace("day", "Day");
        com.alibaba.fastjson.JSONObject jsonObject = WebChatUtil.httpRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
    }

    public void sendImageMessage(String mediaId, String openid) throws Exception {
        String access_token = getAccessToken();
        SendMessageInfo sendMessageInfo = new SendMessageInfo();
        image image = new image();
        sendMessageInfo.setTouser(openid);
        sendMessageInfo.setMsgtype("image");
        image.setMedia_id(mediaId);
        sendMessageInfo.setImage(image);
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;
        String jsonString = JSONObject.fromObject(sendMessageInfo).toString().replace("day", "Day");
        com.alibaba.fastjson.JSONObject jsonObject = WebChatUtil.httpRequest(url, "POST", jsonString);
        System.out.println(jsonObject);
    }

    public WechatUserInfoEntity getSNSUserInfo(String openid){
        String access_token = getAccessToken();
        WechatUserInfoEntity snsUserInfo = null;
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
        String jsonString = JSONObject.fromObject(snsUserInfo).toString().replace("day", "Day");
        com.alibaba.fastjson.JSONObject jsonObject = WebChatUtil.httpRequest(requestUrl, "POST", null);//发送请求
        if (null != jsonObject) {
            snsUserInfo = new WechatUserInfoEntity();
            // 用户的标识   openid= jsonObject.getString("openid");
            snsUserInfo.setOpenId(jsonObject.getString("openid"));
            // 昵称
            snsUserInfo.setNickname(jsonObject.getString("nickname"));
            // 用户头像
            snsUserInfo.setHeadimgurl(jsonObject.getString("headimgurl"));
        }
        return snsUserInfo;
    }

    public String getAccessToken() {
        String accessToken = "";
        String getTokenSql = "select access_token as accessToken from wx_parameter_info where app_id='" + WebChatConfig.APPID + "' limit 1";
        WxTokenInfo wxTokenInfo = sqlMapper.selectOne(getTokenSql, WxTokenInfo.class);
        if (wxTokenInfo == null) {
            accessToken = WebChatConfig.getToken(WebChatConfig.APPID, WebChatConfig.SECERT).getAccessToken();
            String updTokenSql = "update wx_parameter_info set access_token='" + accessToken + "' where app_id='" + WebChatConfig.APPID + "';";
            sqlMapper.update(updTokenSql);
        } else {
            if ("".equals(wxTokenInfo.getAccessToken()) || "null".equals(wxTokenInfo.getAccessToken())) {
                accessToken = WebChatConfig.getToken(WebChatConfig.APPID, WebChatConfig.SECERT).getAccessToken();
                String updTokenSql = "update wx_parameter_info set access_token='" + accessToken + "' where app_id='" + WebChatConfig.APPID + "';";
                sqlMapper.update(updTokenSql);
            }
            accessToken = wxTokenInfo.getAccessToken();
        }
        return accessToken;
    }
}